package logic;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.DecimalFormat;
import javax.swing.JLabel;
import javax.swing.JPanel;

import data.DbConnection;

/**
 * Class SaldoGraf and its instance variables
 * 
 * @author Niels - niha12ai
 * 
 */
public class SaldoGraf extends JPanel {
	Color color;
	private DbConnection dbConnection;
	private JLabel lbltrans;
	double[] transactions;

	/**
	 * Constructor saldoGraf
	 * 
	 * @param color
	 * @param transactions
	 */
	public SaldoGraf(Color color, double[] transactions) {
		this.color = color;
		dbConnection = new DbConnection();
		this.transactions = transactions;
		setLayout(null);

	}

	/**
	 * PaintComponent method which paints the graph through a algorithm several
	 * variables is defined for use in the algorithm and decimalFormat set the
	 * number of decimals The for loop runs transactions and sets the value of
	 * highest
	 */
	public void paintComponent(Graphics g) {
		int x = 100;
		int y = 900;
		double width = 800;
		double height = 800;
		double highest = 0;
		DecimalFormat fc = new DecimalFormat("#.##");

		for (int i = 0; i < transactions.length; i++) {
			if (transactions[i] > highest) {
				highest = transactions[i];
				g.setColor(Color.WHITE);

			}
		}
		/**
		 * yRelation and xGap defined
		 */
		double yRelation = height / highest;
		double xGap = width / transactions.length;
		/**
		 * draws the axes
		 */
		g.drawLine(x, y, (int) (x + width), y);
		g.drawLine(x, y, x, (int) (y - height));
		g.drawLine(x, y, x - 5, y);
		g.drawLine((int) (x), y, (int) (x), y + 5);
		/**
		 * Draws the actual graph
		 */
		for (int i = 1; i < transactions.length; i++) {
			g.setColor(Color.GREEN);
			g.drawLine((int) (x + xGap * (i - 1)),
					(int) (y - (transactions[i - 1] * yRelation)),
					(int) (x + xGap * i),
					(int) (y - (transactions[i] * yRelation)));
			/**
			 * draws the blue lines vertically
			 */
			g.setColor(Color.BLUE);
			g.drawLine((int) (x + xGap * (i)), y + 5, (int) (x + xGap * (i)),
					(int) (y - height));
			g.setColor(Color.WHITE);
			g.fillOval((int) (x + xGap * i - 4), (int) (y
					- (transactions[i] * yRelation) - 4), 8, 8);

		}
		/**
		 * Draws the blue lines across in the graph
		 */
		for (int i = 110; i < height + (height / 10); i += height / 10) {
			g.setColor(Color.BLUE);
			g.drawLine((int) (x + width), (int) (i - yRelation), x - 5,
					(int) (i - yRelation));
		}
		/**
		 * Label for transaction graph
		 */
		lbltrans = new JLabel(".:Transaction Overview:.");
		lbltrans.setForeground(Color.WHITE);
		lbltrans.setFont(new Font("Helvetica", Font.BOLD, 20));
		lbltrans.setBounds(390, 40, 300, 21);
		add(lbltrans);
		/**
		 * draws String
		 */
		g.drawString("Kr.", x - 60, (int) (y - (height + 20)));
		/**
		 * draws number
		 */
		g.drawString("" + (fc.format(highest)), x - 60, (int) (y
				- (highest * yRelation) + 5));
		/**
		 * draws transaction highest transaction number and divides it down the
		 * Y-axis
		 */
		for (int i = 1; i < 10; i++) {
			g.drawString("" + (fc.format((highest / 10) * i)), x - 60, y
					- (int) (((highest / 10) * i) * yRelation) + 5);
		}

	}

}
